package itheima.d01;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.*;

/**
 * 目标：创建一个服务器，接收客户端发送的数据并打印在控制台。
 */
public class Server {

    public static void main(String[] args) throws Exception {

        ExecutorService pool = new ThreadPoolExecutor(
                30,
                34,
                10,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(10),
                Executors.defaultThreadFactory()
                , new ThreadPoolExecutor.DiscardOldestPolicy()
        );
        //1.创建ServerSocket对象，注册服务器端端口
        ServerSocket ss = new ServerSocket(10086);

        //2.使用ServerSocket对象的accept方法获取服务器端Socket对象
        System.out.println("服务器已启动，等待客户端连接...");


       while ( true){
           Socket serverScoket1 = ss.accept();

           System.out.println("一个客户连接了...："+ serverScoket1.getRemoteSocketAddress());
           pool.execute(new ChatRunnable(serverScoket1));
       }
    }
}
